using System.Collections;
using UnityEngine;

public class TestMovesGameMode : TableTennisGameTypeBase
{
	public bool breakOnSwipe;

	[Range(-1f, 1f)]
	public float x;

	[Range(0f, 1f)]
	public float z = 1f;

	[Range(-1f, 1f)]
	public float playerX;

	public float heightAboveNet = 0.2f;

	public float strength = 0.5f;

	public float spin;

	public bool random = true;

	public float delay = 2f;

	protected TableTennisHumanPlayer humanPlayer;

	private FlightInitialConditionsSearch search = new FlightInitialConditionsSearch();

	private MagnusForceAndAirResistanceSquaredBallFlight symFlight = new MagnusForceAndAirResistanceSquaredBallFlight();

	private Vector3 ballPosition
	{
		get
		{
			return new Vector3(0f, Table.tabletopy + 0.2f, Table.halfLength);
		}
	}

	public override void Init(TableTennisMatchController match, MatchUI ui, MatchParameters.MatchParams matchParams)
	{
		base.match = match;
		isMatchReady = true;
		humanPlayer = match.CreateHumanPlayer(-1, true) as TableTennisHumanPlayer;
	}

	public override void StartMatch(int activePlayer = 0)
	{
		StartCoroutine(ShootBallEvery(1f));
	}

	private IEnumerator ShootBallEvery(float humanDelay)
	{
		while (true)
		{
			if (!random)
			{
				match.ball.flightSequence.ClearStages();
				match.ball.SetKinematic(true);
				match.ball.position = ballPosition;
				SetPlayerPosition();
				yield return new WaitForSeconds(humanDelay);
			}
			ShootBall();
			yield return new WaitForSeconds(delay);
		}
	}

	private void SetPlayerPosition()
	{
		humanPlayer.kineticPlan.CancelAll();
		humanPlayer.transform.position = Vector3.forward * humanPlayer.tableSide * Table.halfLength * 1.2f + Vector3.right * Table.halfwidth * playerX;
	}

	private void ShootBall()
	{
		Vector3 position = ballPosition;
		float num = ((!random) ? x : Random.Range(-1f, 1f));
		Vector3 forwardDirection = (Vector3.forward * humanPlayer.tableSide * Table.halfLength * z + Vector3.right * num * Table.halfwidth - ballPosition).OnGround();
		forwardDirection.Normalize();
		float angle = FlightInitialConditionsSearch.GetAngle(heightAboveNet, 0.02f, position);
		float angularVelocityMagnitude = spin;
		ShootBall(position, forwardDirection, angle, angularVelocityMagnitude, strength);
	}

	public override void OnSwipe(TableTennisPlayerBase player)
	{
		if (breakOnSwipe)
		{
			Debug.Break();
		}
	}

	public override void OnMissedServe()
	{
	}

	public override void OnPlayerHitBall(TableTennisPlayerBase player, HitBallParamsPhysicsFlight hitBallParams)
	{
		if (!(player == null))
		{
		}
	}

	public override void OnBallHitObject(PhysicsBall ball, Vector3 pos, Vector3 vel, HitObjectTag hitObjectTag)
	{
	}

	public override void ContinueFromLastPoint(AwardPointParams lastPoint)
	{
	}

	public void ShootBall(Vector3 position, Vector3 forwardDirection, float angle, float angularVelocityMagnitude, float strength)
	{
		search.SetupWithHumanRanges(forwardDirection, position, symFlight);
		FlightInitialConditionsSearch.MaxResult maxResult = search.FindMaxAcceptableVelocity(angle, angularVelocityMagnitude, FlightInitialConditionsSearch.OptimumSearchType.SearchMin);
		FlightInitialConditionsSearch.MaxResult maxResult2 = search.FindMaxAcceptableVelocity(angle, angularVelocityMagnitude, FlightInitialConditionsSearch.OptimumSearchType.SearchMax);
		float velocityMagnitude = Mathf.Lerp(maxResult.magnitude, maxResult2.magnitude, strength);
		FlightParams initialConditions = search.GetInitialConditions(velocityMagnitude, angle, angularVelocityMagnitude);
		FlightSimulator instance = Singleton<FlightSimulator>.instance;
		FlightSimulator.SequenceSimulationParams sim = FlightSimulator.SequenceSimulationParams.DefaultRecordFlightParams(initialConditions, symFlight);
		PhysicsBall ball = match.ball;
		instance.FillFlightSequence(ball.flightSequence, sim);
		ball.flightSequence.Reset();
		humanPlayer.ReceiveBall(ball);
	}
}
